﻿using System;
using System.Collections.Generic;
using System.Windows;
using Microsoft.Research.DynamicDataDisplay.Charts.Filters;

namespace Microsoft.Research.DynamicDataDisplay.Filters
{
    public sealed class RangeFrequencyFilter : PointsFilterBase
    {

        /// <summary>Visible region in screen coordinates</summary>
        private Rect screenRect;
        decimal displayFactor = (decimal)0.5;


        #region IPointFilter Members

        public override void SetScreenRect(Rect screenRect)
        {
            this.screenRect = screenRect;
        }

        // todo probably use LINQ here.
        public override List<Point> Filter(List<Point> points)
        {
            if (points.Count == 0) return points;

            List<Point> resultPoints = points;
            List<Point> currentChain = new List<Point>();

            
            int numberOfDisplayPoints = (int)( (decimal) screenRect.Width * displayFactor);

            if (resultPoints.Count > numberOfDisplayPoints)
            {
                resultPoints.RemoveRange(numberOfDisplayPoints, resultPoints.Count - numberOfDisplayPoints);
            
            }

            return resultPoints;
        }

        #endregion

        private static Point MinByX(IList<Point> points)
        {
            
            Point minPoint = points[0];
            foreach (Point p in points)
            {
                if (p.X < minPoint.X)
                {
                    minPoint = p;
                }
            }
            return minPoint;
        }

        private static Point MaxByX(IList<Point> points)
        {
            Point maxPoint = points[0];
            foreach (Point p in points)
            {
                if (p.X > maxPoint.X)
                {
                    maxPoint = p;
                }
            }
            return maxPoint;
        }

        private static Point MinByY(IList<Point> points)
        {
            Point minPoint = points[0];
            foreach (Point p in points)
            {
                if (p.Y < minPoint.Y)
                {
                    minPoint = p;
                }
            }
            return minPoint;
        }

        private static Point MaxByY(IList<Point> points)
        {
            Point maxPoint = points[0];
            foreach (Point p in points)
            {
                if (p.Y > maxPoint.Y)
                {
                    maxPoint = p;
                }
            }
            return maxPoint;
        }
    }
}
